import tkinter as tk
from tkinter import filedialog
import sys
import os

sys.path.append('../Service/')  # 添加业务逻辑层和数据访问层代码所在路径/

from Service.service import student_evaluation, single_class_course_exam, single_class_exam_mutiple_course, \
    single_class_multiple_exam, single_class_multiple_course_exam, join_data


def main_menu():
    print("1. 上传文件到数据库")
    print("2. 学生评价")
    print("3. 单班级单科目单次考试")
    print("4. 单班级多科目单次考试")
    print("5. 单班级单科目多次考试")
    print("6. 单班级多科目多次考试")
    print("0. 退出")
    return input("请选择功能: ")


def upload_file():
    file_path = filedialog.askopenfilename()
    if not os.path.isfile(file_path):
        feedback_label.config(text="文件不存在。")
        return

    # 将选择的文件路径填充到文件路径输入框中
    file_path_entry.delete(0, tk.END)
    file_path_entry.insert(0, file_path)

    # 调用join_data()函数将文件数据插入数据库
    success = join_data(file_path)
    if success:
        feedback_label.config(text="上传文件成功。")
    else:
        feedback_label.config(text="上传文件失败。")


def evaluate_student():
    student_id = student_id_entry.get()
    # 调用学生评价功能
    success = student_evaluation(student_id)
    if success:
        feedback_label.config(text="学生评估完成。")
    else:
        feedback_label.config(text="学生评估失败，请输入学生ID。")


def single_class_single_course_exam():
    class_id = class_id_entry.get()
    exam_id = exam_id_entry.get()
    # 调用单班级单科目单次考试功能
    success = single_class_course_exam(class_id, [exam_id])
    if success:
        feedback_label.config(text="分析完成。")
    else:
        feedback_label.config(text="分析失败，请输入ID。")


def try_single_class_multiple_course_exam():
    class_id = class_id_entry.get()
    exam_ids = exam_ids_entry.get().split(",")
    # 调用单班级多科目单次考试功能
    success = single_class_exam_mutiple_course(class_id, exam_ids)
    if success:
        feedback_label.config(text="分析完成。")
    else:
        feedback_label.config(text="分析失败，请输入ID。")


def try_single_class_multiple_exam():
    class_id = class_id_entry.get()
    exam_course = exam_course_entry.get()  # 获取学科名
    # 调用单班级单科目多次考试功能，传递学科名作为参数
    success = single_class_multiple_exam(class_id, exam_course)
    if success:
        feedback_label.config(text="分析完成。")
    else:
        feedback_label.config(text="分析失败，请输入ID和学科名。")


def single_class_multiple_course_multiple_exam():
    class_id = class_id_entry.get()
    # 调用单班级多科目多次考试功能
    success = single_class_multiple_course_exam(class_id)
    if success:
        feedback_label.config(text="分析完成。")
    else:
        feedback_label.config(text="分析失败，请输入ID。")


def on_button_click(choice):
    if choice == "1":
        upload_file()
    elif choice == "2":
        evaluate_student()
    elif choice == "3":
        single_class_single_course_exam()
    elif choice == "4":
        class_id = class_id_entry.get()
        exam_ids = exam_ids_entry.get().split(",")
        # 调用单班级多科目单次考试功能
        single_class_exam_mutiple_course(class_id, exam_ids)
    elif choice == "5":
        class_id = class_id_entry.get()
        exam_course = exam_course_entry.get()
        single_class_multiple_exam(class_id, exam_course)
    elif choice == "6":
        class_id = class_id_entry.get()
        single_class_multiple_course_exam(class_id)
    elif choice == "0":
        print("程序已退出。")
        root.quit()
    else:
        print("无效的选择，请重新输入。")


def create_button(frame, text, choice):
    button = tk.Button(frame, text=text, command=lambda: on_button_click(choice))
    button.pack(side=tk.LEFT, padx=5, pady=5)


if __name__ == "__main__":
    root = tk.Tk()
    root.title("Score Analysis System")

    main_frame = tk.Frame(root)
    main_frame.pack(padx=10, pady=10)

    menu_frame = tk.Frame(main_frame)
    menu_frame.pack()

    create_button(menu_frame, "上传文件到数据库", "1")
    create_button(menu_frame, "学生评价", "2")
    create_button(menu_frame, "单班级单科目单次考试", "3")
    create_button(menu_frame, "单班级多科目单次考试", "4")
    create_button(menu_frame, "单班级单科目多次考试", "5")
    create_button(menu_frame, "单班级多科目多次考试", "6")
    create_button(menu_frame, "退出", "0")

    input_frame = tk.Frame(main_frame)
    input_frame.pack(padx=10, pady=10)

    class_id_label = tk.Label(input_frame, text="班级ID:")
    class_id_label.grid(row=1, column=0, pady=5)
    class_id_entry = tk.Entry(input_frame)
    class_id_entry.grid(row=1, column=1, pady=5)

    exam_id_label = tk.Label(input_frame, text="考试ID:")
    exam_id_label.grid(row=2, column=0, pady=5)
    exam_id_entry = tk.Entry(input_frame)
    exam_id_entry.grid(row=2, column=1, pady=5)

    exam_ids_label = tk.Label(input_frame, text="多次考试ID（逗号分隔）:")
    exam_ids_label.grid(row=3, column=0, pady=5)
    exam_ids_entry = tk.Entry(input_frame)
    exam_ids_entry.grid(row=3, column=1, pady=5)

    exam_course_label = tk.Label(input_frame, text="考试课程名:")
    exam_course_label.grid(row=4, column=0, pady=5)
    exam_course_entry = tk.Entry(input_frame)
    exam_course_entry.grid(row=4, column=1, pady=5)

    student_id_label = tk.Label(input_frame, text="学生ID:")
    student_id_label.grid(row=0, column=0, pady=5)
    student_id_entry = tk.Entry(input_frame)
    student_id_entry.grid(row=0, column=1, pady=5)

    file_path_label = tk.Label(input_frame, text="文件路径:")
    file_path_label.grid(row=5, column=0, pady=5)
    file_path_entry = tk.Entry(input_frame)
    file_path_entry.grid(row=5, column=1, pady=5)

    feedback_label = tk.Label(main_frame, text="", fg="red")
    feedback_label.pack(pady=10)

    root.mainloop()
